package com.r2coding.ent.controller;

import com.r2coding.common.enums.Module;
import com.r2coding.common.enums.OperationType;
import com.r2coding.ent.annotation.SysLog;
import org.springframework.beans.factory.annotation.Autowired;

import com.r2coding.ent.entity.AlbumEntity;
import com.r2coding.ent.service.AlbumService;
import com.r2coding.common.util.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * 相册
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-06-20 19:59:36
 */
@RestController
@RequestMapping("/album")
public class AlbumController {
    @Autowired
    private AlbumService albumService;

    /**
     * 保存
     */
    @SysLog(module = Module.ENT, operationType = OperationType.INSERT, description = "添加相册")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('album:add')")
    public Result save(@RequestBody AlbumEntity album) {
        album.setDeleted(0);
        album.setUpdateTime(new Date());
        album.setCreateTime(new Date());
        album.setThumhup(0);
        // 获取用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails)authentication.getDetails();
        Map<String, Object> map = (Map<String, Object>)details.getDecodedDetails();
        Map<String, Object> userInfo = (Map<String, Object>)map.get("userInfo");
        Integer userId = (Integer)userInfo.get("userId");
        album.setCreater(userId);
		albumService.save(album);
        return Result.ok();
    }

    /**
     * 修改
     */
    @SysLog(module = Module.ENT, operationType = OperationType.UPDATE, description = "修改相册")
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('album:edit')")
    public Result update(@RequestBody AlbumEntity album){
        album.setUpdateTime(new Date());
		albumService.updateById(album);
        return Result.ok();
    }

    /**
     * 删除
     */
    @SysLog(module = Module.ENT, operationType = OperationType.DELETE, description = "删除相册")
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('album:delete')")
    public Result delete(@PathVariable("id") Integer id){
		albumService.removeById(id);
        return Result.ok();
    }
}
